package com.cloud.user.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * DruidDBConfig类被@Configuration标注，用作配置信息；
 * DataSource对象被@Bean声明，为Spring容器所管理，
 * @Primary表示这里定义的DataSource将覆盖其他来源的DataSource。
 * @author ZSX
 *jdbc.url=${jdbc.url}
 *最新的支持方式如下:
 *jdbc.url=@jdbc.url@
 */
@Configuration
@RefreshScope
public class DruidDBConfig {
    private Logger logger = LoggerFactory.getLogger(DruidDBConfig.class);
    @Bean // 声明其为Bean实例
    @Primary // 在同样的DataSource中，首先使用被标注的DataSource
    @ConfigurationProperties(prefix ="spring.datasource")
    @RefreshScope
    public DataSource dataSource() {
        DruidDataSource dataSource = BeanUtils.instantiate(DruidDataSource.class);
        System.out.println("data source init success");
        return dataSource;
    }
}
